<?php

/**
 * @package Tplugin
 */
/*
  Plugin Name: Tplugin
  Plugin URI: http://itvnn.com.vn/
  Description: Tplugin
  Version: 1.0
  Author: Nguyen Dinh Tiep
  Author URI: http://automattic.com/wordpress-plugins/
  License: GPLv2 or later
  Text Domain: Tplugin
 */
add_action('wp_enqueue_scripts', 'tiep_scripts');

function tiep_scripts() {
    wp_enqueue_script('tplugin-ajax', plugin_dir_url(__FILE__) . 'js/tajax.js', array(), '1.0.0', true);
//tiep scripts
    wp_localize_script('tplugin-ajax', 'bct_vars', array(
        'ajax_url' => admin_url('admin-ajax.php'),
        'bct_nonce' => wp_create_nonce('bct_nonce')
    ));
}

add_action('init', 'tplugin_posttype_init');

function tplugin_posttype_init() {


//News
    $args = array(
        'labels' => array('name' => 'News'),
        'public' => true,
        'publicly_queryable' => true,
        'show_ui' => true,
        'show_in_menu' => true,
        'query_var' => true,
        'rewrite' => array('slug' => 'news'),
        'capability_type' => 'post',
        'has_archive' => true,
        'hierarchical' => true,
        'supports' => array('title', 'editor')
    );
    register_post_type('news', $args);
    // Diễn Giả
    $args = array(
        'labels' => array('name' => 'Diễn Giả'),
        'public' => true,
        'publicly_queryable' => true,
        'show_ui' => true,
        'show_in_menu' => true,
        'query_var' => true,
        'rewrite' => array('slug' => 'diengia'),
        'capability_type' => 'post',
        'has_archive' => true,
        'hierarchical' => true,
        'supports' => array('title', 'editor')
    );
    register_post_type('diengia', $args);
    // Kinh nghiệm khởi nghiệp
    $args = array(
        'labels' => array('name' => 'Kinh nghiệp khởi nghiệp'),
        'public' => true,
        'publicly_queryable' => true,
        'show_ui' => true,
        'show_in_menu' => true,
        'query_var' => true,
        'rewrite' => array('slug' => 'kinhnghiem'),
        'capability_type' => 'post',
        'has_archive' => true,
        'hierarchical' => true,
        'supports' => array('title', 'editor')
    );
    register_post_type('kinhnghiem', $args);
    // Thông tin người tìm việc
    $args = array(
        'labels' => array('name' => 'Thông tin người tìm việc'),
        'public' => true,
        'publicly_queryable' => true,
        'show_ui' => true,
        'show_in_menu' => true,
        'query_var' => true,
        'rewrite' => array('slug' => 'nguoitimviec'),
        'capability_type' => 'post',
        'has_archive' => true,
        'hierarchical' => true,
        'supports' => array('title', 'editor')
    );
    register_post_type('nguoitimviec', $args);
}

add_action('init', 'tplugin_new_tax');

function tplugin_new_tax() {
    register_taxonomy(
            'news-category', 'news', array(
        'label' => __('News Category'),
        'rewrite' => array('slug' => 'news-category'),
        'hierarchical' => true,
            )
    );
}

//Contact form
add_action('wp_ajax_form_tim_viec', 'form_tim_viec_ajax');
add_action('wp_ajax_nopriv_form_tim_viec', 'form_tim_viec_ajax');

function form_tim_viec_ajax() {
    $arr = array();
    $arr['error'] = 0;
    if (!isset($_POST['bct_nonce']) || !wp_verify_nonce($_POST['bct_nonce'], 'bct_nonce')) {
        $arr['error'] = 1;
        $arr['message'] = 'Please try again!';
        die(json_encode($arr));
    }
    $name = sql_injecttion($_POST['hvtname']);
    $gioitinh = sql_injecttion($_POST['gioitinh']);
    $birthday = sql_injecttion($_POST['birthday']);
    $cmt = sql_injecttion($_POST['cmt']);
    $email = sql_injecttion($_POST['email']);
    $phone = sql_injecttion($_POST['phone']);
    $address = sql_injecttion($_POST['address']);
    $schools = sql_injecttion($_POST['schools']);
    $bachocnn = sql_injecttion($_POST['bachocnn']);
    $bachocth = sql_injecttion($_POST['bachocth']);
    $vitrimongmuon = sql_injecttion($_POST['vitrimongmuon']);
    $mucluongmongmuon = sql_injecttion($_POST['mucluongmongmuon']);
    $nganhnghequantam = sql_injecttion($_POST['nganhnghequantam']);
    $methodwork = sql_injecttion($_POST['methodworking']);

    if ($name == '' || $gioitinh == '' || $birthday == '' || $cmt == '' || $email == '' || $phone == '' || $address == '' || !filter_var($email, FILTER_VALIDATE_EMAIL)) {
        $arr['error'] = 1;
        $arr['message'] = '<span style="color:red">Vui lòng nhập đúng yêu cấu!</span>';
        die(json_encode($arr));
    }
    $bctpost = array(
        'post_title' => $name,
        'post_type' => 'nguoitimviec'
    );

    $pid = wp_insert_post($bctpost);
    add_post_meta($pid, 'wit_nguoitimviec_sex', $gioitinh);
    add_post_meta($pid, 'wit_nguoitimviec_birthday', $birthday);
    add_post_meta($pid, 'wit_nguoitimviec_cmt', $cmt);
    add_post_meta($pid, 'wit_nguoitimviec_email', $email);
    add_post_meta($pid, 'wit_nguoitimviec_phone', $phone);
    add_post_meta($pid, 'wit_nguoitimviec_address', $address);
    add_post_meta($pid, 'wit_nguoitimviec_schools', $schools);
    add_post_meta($pid, 'wit_nguoitimviec_trinhdonn', $bachocnn);
    add_post_meta($pid, 'wit_nguoitimviec_trinhdoth', $bachocth);
    add_post_meta($pid, 'wit_nguoitimviec_likeaddress', $vitrimongmuon);
    add_post_meta($pid, 'wit_nguoitimviec_money', $mucluongmongmuon);
    add_post_meta($pid, 'wit_nguoitimviec_working', $nganhnghequantam);
    add_post_meta($pid, 'wit_nguoitimviec_methodworking', $methodwork);
    $arr['message'] = '<span style="color:green">Thanks for your registor!</span>';

    die(json_encode($arr));
}
